package com.ruoyi.project.newhope.inspection.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.newhope.audio.domain.NewHopeAudio;
import com.ruoyi.project.newhope.audio.mapper.NewHopeAudioMapper;
import com.ruoyi.project.newhope.data.mapper.NewHopeCollectDataMapper;
import com.ruoyi.project.newhope.warning.domain.NewHopeWarning;
import com.ruoyi.project.newhope.warning.mapper.NewHopeWarningMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.newhope.inspection.mapper.NewHopeInspectionMapper;
import com.ruoyi.project.newhope.inspection.domain.NewHopeInspection;
import com.ruoyi.project.newhope.inspection.service.INewHopeInspectionService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 化验项目设置Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-02-08
 */
@Service
public class NewHopeInspectionServiceImpl implements INewHopeInspectionService 
{
    @Autowired
    private NewHopeInspectionMapper newHopeInspectionMapper;
    @Autowired
    private NewHopeCollectDataMapper newHopeCollectDataMapper;
    @Autowired
    private NewHopeAudioMapper newHopeAudioMapper;
    @Autowired
    private NewHopeWarningMapper newHopeWarningMapper;

    /**
     * 查询化验项目设置
     * 
     * @param id 化验项目设置ID
     * @return 化验项目设置
     */
    @Override
    public NewHopeInspection selectNewHopeInspectionById(Long id)
    {
        NewHopeInspection inspection = newHopeInspectionMapper.selectNewHopeInspectionById(id);
        if (inspection.getAudioId() != null) {
            NewHopeAudio newHopeAudio = newHopeAudioMapper.selectNewHopeAudioById(inspection.getAudioId());
            if (newHopeAudio!=null) {
                inspection.setAudioName(newHopeAudio.getAudioName());
                inspection.setAudioUrl(newHopeAudio.getAudioUrl());
            }
        }
        return inspection;
    }

    /**
     * 查询化验项目设置列表
     * 
     * @param newHopeInspection 化验项目设置
     * @return 化验项目设置
     */
    @Override
    public List<NewHopeInspection> selectNewHopeInspectionList(NewHopeInspection newHopeInspection)
    {
        List<NewHopeInspection> newHopeInspections = newHopeInspectionMapper.selectNewHopeInspectionList(newHopeInspection);
        for (NewHopeInspection inspection : newHopeInspections) {
            if (inspection.getAudioId() != null) {
                NewHopeAudio newHopeAudio = newHopeAudioMapper.selectNewHopeAudioById(inspection.getAudioId());
                if (newHopeAudio!=null) {
                    inspection.setAudioName(newHopeAudio.getAudioName());
                    inspection.setAudioUrl(newHopeAudio.getAudioUrl());
                }
            }
        }
        return newHopeInspections;
    }

    /**
     * 新增化验项目设置
     * 
     * @param newHopeInspection 化验项目设置
     * @return 结果
     */
    @Override
    public int insertNewHopeInspection(NewHopeInspection newHopeInspection)
    {
        newHopeInspection.setCreateTime(DateUtils.getNowDate());
        return newHopeInspectionMapper.insertNewHopeInspection(newHopeInspection);
    }

    /**
     * 修改化验项目设置
     * 
     * @param newHopeInspection 化验项目设置
     * @return 结果
     */
    @Override
    public int updateNewHopeInspection(NewHopeInspection newHopeInspection)
    {
        NewHopeInspection old = newHopeInspectionMapper.selectNewHopeInspectionById(newHopeInspection.getId());

        newHopeInspection.setUpdateTime(DateUtils.getNowDate());
        int i = newHopeInspectionMapper.updateNewHopeInspection(newHopeInspection);
        if (i == 1 && !Objects.equals(newHopeInspection.getInspectionCode(), old.getInspectionCode())) {
            //修改 collect_data 中所有指标值的
            newHopeCollectDataMapper.updateInspectionCode(old.getInspectionCode(), newHopeInspection.getInspectionCode());
        }
        if (i == 1 && newHopeInspection.getStepSwitch() != null) {
            // 代表用户手动关闭报警，检测当前指标是否存在于告警中，将告警状态置为不在告警
            List<NewHopeWarning> warnings = newHopeWarningMapper.selectNewHopeWarningByInspectionId(newHopeInspection.getId());
            if (warnings != null && warnings.size() > 0) {
                newHopeWarningMapper.updateNewHopeWarningSwitchByInspectionId(newHopeInspection.getId(), newHopeInspection.getStepSwitch());
            }
        }
        return i;
    }

    /**
     * 删除化验项目设置对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNewHopeInspectionByIds(String ids)
    {
        return newHopeInspectionMapper.deleteNewHopeInspectionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除化验项目设置信息
     * 
     * @param id 化验项目设置ID
     * @return 结果
     */
    @Override
    public int deleteNewHopeInspectionById(Long id)
    {
        return newHopeInspectionMapper.deleteNewHopeInspectionById(id);
    }

    @Override
    public Integer isExist(NewHopeInspection newHopeInspection) {
        return newHopeInspectionMapper.isExist(newHopeInspection);
    }
}
